<template>
  <div class="requests-list">
    <div v-if="filteredRequests.length === 0" class="empty-state">
      <div class="empty-icon">📡</div>
      <div class="empty-text">
        {{ store.searchKeyword ? '未找到匹配的请求' : '暂无网络请求' }}
      </div>
      <div class="empty-hint">
        {{ store.searchKeyword ? '尝试修改搜索关键词' : '刷新页面或进行操作来捕获网络请求' }}
      </div>
    </div>
    
    <div v-else>
      <div 
        v-for="request in filteredRequests" 
        :key="request.id"
        class="request-item"
        @click="addToIntercept(request.url, request.method)"
      >
        <div class="request-method" :class="request.method.toLowerCase()">
          {{ request.method }}
        </div>
        <div class="request-url" :title="request.url">
          {{ request.url }}
        </div>
        <div class="request-time">
          {{ formatTime(request.timestamp) }}
        </div>
        <div v-if="request.intercepted" class="request-status intercepted">
          已拦截
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { computed } from 'vue'
import { useInterceptorStore } from '@/stores/interceptor'
import { ElMessage } from 'element-plus'
import { debounce } from 'lodash-es'

const emit = defineEmits(['openEditor'])
const store = useInterceptorStore()

// 过滤请求列表
const filteredRequests = computed(() => {
  return store.getFilteredRequests()
})

// 添加到拦截列表
const addToIntercept = debounce((url, method) => {
  console.log('🔧 addToIntercept called with URL:', url)

  // 检查是否已存在
  const existing = store.interceptedRequests.find(item => item.url === url)
  if (existing) {
    ElMessage.warning('该请求已在拦截列表中')
    return
  }

  // 从请求列表中找到对应的请求，获取其响应数据
  const request = store.requests.find(req => req.url === url && req.method === method)
  let originalData = '{"message": "暂无响应数据，请手动输入"}'

  if (request && request.responseData) {
    originalData = request.responseData
  }

  // 显示提示信息
  ElMessage.info('正在打开编辑器...')

  // 直接打开编辑器让用户编辑，传递完整的请求数据
  emit('openEditor', {
    url,
    method,
    responseData: originalData,
    requestHeaders: request.requestHeaders || {},
    requestBody: request.requestBody || null
  })
}, 500)

// 格式化时间
const formatTime = (timestamp) => {
  const date = new Date(timestamp)
  return date.toLocaleTimeString()
}
</script>

<style scoped>
.requests-list {
  height: 100%;
}

.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 100%;
  color: #6c757d;
  text-align: center;
}

.empty-icon {
  font-size: 48px;
  margin-bottom: 16px;
  opacity: 0.5;
}

.empty-text {
  font-size: 16px;
  font-weight: 500;
  margin-bottom: 8px;
}

.empty-hint {
  font-size: 12px;
  opacity: 0.7;
}

.request-item {
  display: flex;
  align-items: center;
  padding: 12px;
  border-bottom: 1px solid #e9ecef;
  cursor: pointer;
  transition: background 0.2s;
  gap: 12px;
}

.request-item:hover {
  background: #f8f9fa;
}

.request-method {
  padding: 4px 8px;
  border-radius: 4px;
  font-size: 10px;
  font-weight: bold;
  color: white;
  min-width: 50px;
  text-align: center;
}

.request-method.get { background: #28a745; }
.request-method.post { background: #007bff; }
.request-method.put { background: #ffc107; color: #000; }
.request-method.delete { background: #dc3545; }
.request-method.patch { background: #6f42c1; }

.request-url {
  flex: 1;
  font-size: 12px;
  color: #495057;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.request-time {
  font-size: 11px;
  color: #6c757d;
  white-space: nowrap;
}

.request-status.intercepted {
  padding: 2px 6px;
  background: #28a745;
  color: white;
  border-radius: 3px;
  font-size: 10px;
}
</style>
